package org.darcy.service.sys.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.darcy.entity.sys.SysArea;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.service.sys.SysAreaService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Service
public class SysAreaServiceImpl extends GenericServiceImpl<SysArea> implements SysAreaService {

	@Override
	public SysArea getDetail(Integer id) {
		SysArea record = super.getById(id);
		return record;
	}

	@Override
	public PageData<SysArea> page(PageParam<SysArea> param) {		
		QueryWrapper<SysArea> wrapper = new QueryWrapper<SysArea>();
		SysArea query = param.getEntity();
		if(query.getLevel()!=null) {
			wrapper.eq("level", query.getLevel());
		}
		if(query.getParentCode()!=null) {
			wrapper.eq("parent_code", query.getParentCode());
		}
		if(query.getKeyword()!=null) {
			wrapper.like("name", query.getKeyword());
		}
		IPage page = new Page(param.getQuery().getPageNum(), param.getQuery().getPageSize());
		IPage list = this.getPage(page, wrapper);
		PageData data = new PageData(list, param);
		return data;
	}

	@Override
	public List<SysArea> getChildren(String pcode) {
		LambdaQueryWrapper<SysArea> areaLambdaQueryWrapper = new LambdaQueryWrapper<>();
		if (StringUtils.isNotBlank(pcode)) {
			areaLambdaQueryWrapper.eq(SysArea::getParentCode, pcode.trim());
		} else {
			areaLambdaQueryWrapper.isNull(SysArea::getParentCode).or().eq(SysArea::getParentCode, "");
		}
		areaLambdaQueryWrapper.orderByAsc(SysArea::getAreaCode);
		return this.getList(areaLambdaQueryWrapper);
	}

}
